//
//  WelcomeViewController.swift
//  Weibo
//
//  Created by apple on 15/12/21.
//  Copyright © 2015年 Leon. All rights reserved.
//

import UIKit
import SnapKit
import SDWebImage

/*
自动布局的页面实现动画效果需要注意
1.不能够直接修改frame,可能会引起自动布局系统计算错误
2.直接修改视图的约束关系.自动布局系统会根据约束关系 自动计算空间的frame  在layoutSubViews方法中计算视图的frame
3.每一次运行循环开启时  自动布局系统都会  '收集' 所有页面视图的约束的修改 不会立即更新约束
在运行循环结束前  会自动调用 layoutsubViews方法 修改所有子视图的frame

4.如果希望提前更新约束 需要强制刷新页面 self.view.layoutIfNeeded() 需要在动画的闭包中执行 并且 一定要在修改约束之后调用
*/


class WelcomeViewController: UIViewController {

    override func loadView() {
        view = backImageView
    }
    override func viewDidLoad() {
        super.viewDidLoad()
        setupUI()
        
    }
    override func viewDidAppear(animated: Bool) {
        self.showAnimation()
    }
    private func setupUI() {
        view.addSubview(iconView)
        view.addSubview(welcomeLabel)
        
        //设置约束
        iconView.snp_makeConstraints { (make) -> Void in
            make.centerX.equalTo(view.snp_centerX)
            make.bottom.equalTo(view.snp_bottom).offset(-200)
            
            make.size.equalTo(CGSizeMake(90, 90))
        }
        
        welcomeLabel.snp_makeConstraints { (make) -> Void in
            make.centerX.equalTo(iconView.snp_centerX)
            make.top.equalTo(iconView.snp_bottom).offset(16)
        }
        
        iconView.sd_setImageWithURL(UserAccountViewModel().headURL, placeholderImage: UIImage(named: "avatar_default_big"))
        //裁剪头像
        iconView.layer.cornerRadius = 45
        iconView.layer.masksToBounds = true
    }
    
    private func showAnimation() {
        welcomeLabel.alpha = 0
        
        let offset = -UIScreen.mainScreen().bounds.height + 200
        
        UIView.animateWithDuration(1.5, delay: 0, usingSpringWithDamping: 0.98, initialSpringVelocity: 9.8, options: [], animations: { () -> Void in
            
            self.iconView.snp_updateConstraints { (make) -> Void in
                make.bottom.equalTo(self.view.snp_bottom).offset(offset)
            }
            
            //强制刷新页面 刷新frame
            self.view.layoutIfNeeded()
            }) { (_) -> Void in
                UIView.animateWithDuration(0.5, animations: { () -> Void in
                    self.welcomeLabel.alpha = 1
                    }, completion: { (_) -> Void in
                        //动画结束的时候 发出 页面切换的通知
                        NSNotificationCenter.defaultCenter().postNotificationName(WBSwitchRootVCNotification, object: nil)
                        print("OK")
                })
        }
    }
    //懒加载所有子视图
    private lazy var backImageView: UIImageView = UIImageView(image: UIImage(named: "ad_background"))
    private lazy var iconView: UIImageView = UIImageView()
    private lazy var welcomeLabel: UILabel = {
        let l = UILabel()
        l.text = ( UserAccountViewModel().userName ?? "" ) + " 欢迎回来"
        l.font = UIFont.systemFontOfSize(16)
        l.textColor = UIColor.lightGrayColor()
        //设置大小
        l.sizeToFit()
        return l
    }()

}
